package com.daydayup.learn.zikao;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;

/**
 * 编写一个GUI程序，程序允许用户在窗体上执行按下鼠标，拖动鼠标和松开鼠标的动作，
 * 在动作结束时，程序以适当的顶点坐标、长度和宽度绘制一个椭圆
 */
public class DrawEllipseDemo extends JPanel {
    private java.awt.Point startPint;
    private java.awt.Point endPoint;
    private Ellipse2D ellipse;

    public DrawEllipseDemo() {
        setBackground(Color.WHITE);
        //setTitle("绘制椭圆演示");
        setSize(800,600);
        //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //setLocationRelativeTo(null);
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                System.out.println("鼠标按下了");
                startPint = e.getPoint();
                endPoint = startPint;
                ellipse = null;
                repaint();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                System.out.println("鼠标释放了");
                endPoint = e.getPoint();
                int x = Math.min(startPint.x, endPoint.x);
                int y = Math.min(startPint.y, endPoint.y);
                int width = Math.abs(endPoint.x - startPint.x);
                int height = Math.abs(endPoint.y - startPint.y);
                ellipse = new Ellipse2D.Double(x,y,width,height);
                repaint();
            }
        });
        addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseDragged(MouseEvent e) {
                endPoint = e.getPoint();
                repaint();
            }

            @Override
            public void mouseMoved(MouseEvent e) {

            }
        });
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        // 抗锯齿设置
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        // 如果椭圆已经确定,绘制它
        if(ellipse != null) {
            g2d.draw(ellipse);
        } else {
            // 在拖动的过程中，绘制一个临时的矩形框
            if (startPint != null && endPoint != null) {
                int x = Math.min(startPint.x, endPoint.x);
                int y = Math.min(startPint.y, endPoint.y);
                int width = Math.abs(endPoint.x - startPint.x);
                int height = Math.abs(endPoint.y - startPint.y);
                g2d.draw(new Rectangle2D.Double(x, y, width, height));
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("绘制椭圆演示");
            DrawEllipseDemo demo = new DrawEllipseDemo();
            frame.add(demo);
            frame.setSize(800,600);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }
}
